home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Mail
/
pine3.92
/
pine
/
osdep
/
readfile.os2
< prev
next >
Wrap
Text File
|
1996-03-14
|
2KB
|
63 lines
#line 2 "osdep/readfile.os2"
/*----------------------------------------------------------------------
Read whole file into memory
Args: filename -- path name of file to read
Result: Returns pointer to malloced memory with the contents of the file
or NULL
This won't work very well if the file has NULLs in it and is mostly
intended for fairly small text files.
This is a slightly modified version for OS/2 which allows us to do some
sexy things that might not be possible otherwise, such as running a
program to do signatures and so forth. To run a program via a pipe,
just precede the command with a |.
----*/
char *
read_file(filename)
char *filename;
{
int nb;
char *buf =NULL;
if (*filename != '|') {
int fd;
struct stat statbuf;
if ((fd = open(filename, O_RDONLY|O_BINARY)) >= 0) {
fstat(fd, &statbuf);
buf = fs_get((size_t)statbuf.st_size + 1);
if((nb = read(fd, buf, (int)statbuf.st_size)) < 0)
fs_give((void **)&buf); /* NULL's buf */
else
buf[nb] = '\0';
close(fd);
}
}
else{
PIPE_S * sp = open_system_pipe(filename+1, NULL, NULL, PIPE_READ);
if (sp){
char *p = tmp_20k_buf;
char tmp[1024];
while (fgets(tmp, sizeof tmp - 1, sp->ifilep)!=NULL) {
int len = strlen(tmp);
if (p - tmp_20k_buf + len > 20480)
break;
memcpy(p, tmp, len);
p += len;
}
*p++ = '\0';
nb = p - tmp_20k_buf;
close_system_pipe(&sp);
buf = fs_get(nb);
memcpy(buf, tmp_20k_buf, nb);
}
}
return buf;
}